package com.java1234.controller;

import com.java1234.entity.Leave;
import com.java1234.entity.PageBean;
import com.java1234.service.LeaveService;
import com.java1234.util.ResponseUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dengwubo on 2017/8/25.
 */
@Controller
@RequestMapping("/leave")
public class LeaveController {

    @Autowired
    private LeaveService leaveService;
    @Resource
    private RuntimeService runtimeService;

    @Resource
    private TaskService taskService;

    @RequestMapping("/list")
    public String list(@RequestParam(value="page",required=false)String page, @RequestParam(value="rows",required=false)String rows,
                       String userId, HttpServletResponse response)throws Exception{

        PageBean pageBean = new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("userId",userId);
        map.put("start",pageBean.getStart());
        map.put("size",pageBean.getPageSize());
        List<Leave>leaveList = leaveService.find(map);
        Long total = leaveService.getTotal(map);
        JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.registerJsonValueProcessor(Date.class,new DateJsonValueProcessor("yyyy-MM-dd hh:mm:ss"));
        JSONObject result = new JSONObject();
        JSONArray jsonArray = JSONArray.fromObject(leaveList,jsonConfig);
        result.put("rows",jsonArray);
        result.put("total",total);
        ResponseUtil.write(response,result);
        return null;
    }


    @RequestMapping("/save")
    public String save(Leave leave,HttpServletResponse response,Integer flag)throws Exception{
        int resultTotal = 0;
        leave.setLeaveDate(new Date());
        resultTotal = leaveService.add(leave);

        JSONObject result= new JSONObject();
        if (resultTotal>0){
            result.put("success",true);
        }else{
            result.put("success",false);
        }
        ResponseUtil.write(response,result);
        return null;
    }

    @RequestMapping("/startApply")
    public String startApply(Integer leaveId,HttpServletResponse response)throws Exception{
        Map<String,Object> variables = new HashMap<>();
        variables.put("leaveId",leaveId);
        ProcessInstance pi = runtimeService.startProcessInstanceByKey("studentLeaveProcess",variables); //启动流程实例
        Task task=taskService.createTaskQuery().processInstanceId(pi.getProcessInstanceId()).singleResult();// 根据流实例Id查询任务
        taskService.complete(task.getId());// 完成学生填写请假单任务
        Leave leave = leaveService.findById(leaveId);
        leave.setState("审核中");
        leave.setProcessInstanceId(pi.getProcessInstanceId());
        leaveService.update(leave);  // 修改请假单状态
        JSONObject result = new JSONObject();
        result.put("success",true);
        ResponseUtil.write(response,result);
        return null;
    }

    @RequestMapping("/getLeaveByTaskId")
    public String getLeaveByTaskId(String taskId,HttpServletResponse response)throws Exception{
        Integer leaveId = (Integer) taskService.getVariable(taskId,"leaveId"); // 根据taskId 与 key leaveId 获取 leaveId
        Leave leave = leaveService.findById(leaveId);
        JSONObject result = new JSONObject();
        result.put("leave",JSONObject.fromObject(leave));
        ResponseUtil.write(response,result);
        return null;
    }


}
